//
//  ProductCell.swift
//  BaseSwift
//
//  Created by gh on 2019/10/12.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class ProductCell: UITableViewCell {

    let containerView = UIView()
    
    let photo = UIImageView()
    let nameLab = UILabel()
    let startLab = UILabel()

    var tagsView:STTagsView? = nil
    let priceLab = UILabel()
    
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.contentView.addSubview(containerView)
        self.containerView.sd_cornerRadius = 8
        self.backgroundColor = UIColor.backgroundColor
        containerView.backgroundColor = UIColor.white
        self.selectionStyle = .none

        let _ = containerView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10))

        // shadowCode
        containerView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
        containerView.layer.shadowOffset = CGSize(width: 0, height: 0)
        containerView.layer.shadowOpacity = 1
        containerView.layer.shadowRadius = 20
        
        let height = (KScreenWidth-20)*9/16
        
        containerView.addSubview(photo)
        photo.backgroundColor = UIColor.cccColor
        let _ = photo.sd_layout()?.heightIs(CGFloat(height))?.widthRatioToView(containerView,1)?.topSpaceToView(containerView,0)
        containerView.clipsToBounds = true
        
        let startView = UIView()
        containerView.addSubview(startView)
        startView.backgroundColor = UIColor.textTitleColor
        let _ = startView.sd_layout()?.widthIs(125)!.heightIs(24)!.leftSpaceToView(containerView,0)!.topSpaceToView(containerView,0)
        startView.updateLayout()
        let maskPath = UIBezierPath.init(roundedRect: startView.bounds,

        byRoundingCorners: [UIRectCorner.topLeft, UIRectCorner.bottomRight],

        cornerRadii: CGSize(width: 8, height: 8))

        let maskLayer = CAShapeLayer()

        maskLayer.frame = startView.bounds

        maskLayer.path = maskPath.cgPath

        startView.layer.mask = maskLayer
        
        startView.addSubview(self.startLab)
        self.startLab.textColor = UIColor.white
        self.startLab.font = UIFont.atLeastSmallFont
        let _ = startLab.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5))
        
        
        containerView.addSubview(nameLab)
        nameLab.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight(rawValue: 0.2))
        nameLab.textColor = UIColor.textTitleColorBlack
        nameLab.text = "阿根廷、南极午夜阳号18天之旅"
        let _ = nameLab.sd_layout()?.leftSpaceToView(containerView,16)?.autoHeight(0).rightSpaceToView(containerView,16)?.topSpaceToView(photo,10)
        nameLab.setMaxNumberOfLinesToShow(2)
        
        
        nameLab.updateLayout()
        containerView.updateLayout()
        tagsView = STTagsView.tagView(withFrame: CGRect(x: 10, y: nameLab.frame.maxY+2, width: containerView.width-34, height: 15), tagsArray: ["打卡阿根廷网红地","一天","线路"], textColor: UIColor.GP_Blue_Color, textFont: UIFont .systemFont(ofSize: 10), normalTagBackgroundColor: UIColor.backgroundColor, tagBorderColor: UIColor.GP_Blue_Color, contentInsets: UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5), labelContentInsets: UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3), labelHorizontalSpacing: 5, labelVerticalSpacing: 5)
        containerView.addSubview(tagsView!)
        
        containerView.addSubview(priceLab)
        priceLab.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight(rawValue: 0.2))
        priceLab.textColor = UIColor.GP_Orange_Color
        priceLab.text = "￥900.00起"
        let _ = priceLab.sd_layout()?.leftSpaceToView(containerView,10)?.heightIs(22)?.rightSpaceToView(containerView,10)?.bottomSpaceToView(containerView,10)
        
        
    }

    func setModel(mod:ProductVo){
        
        photo.sd_setImage(with: URL(string:mod.pdtImg!), placeholderImage: UIImage(named: "holder_big"), options: .lowPriority) { (img, B, C, D) in
         
            SDImageCache.shared.store(img, forKey: mod.pdtImg!, toDisk: true) {
                 
             }
        }
        nameLab.text = mod.pdtName

        nameLab.updateLayout()

        tagsView?.removeFromSuperview()
        let tags = mod.tags?.split(string: ",")
        tagsView = STTagsView.tagView(withFrame: CGRect(x: 13, y: nameLab.frame.maxY+2, width: containerView.width-40, height: 15), tagsArray: tags, textColor: UIColor.GP_Blue_Color, textFont: UIFont .systemFont(ofSize: 10), normalTagBackgroundColor: UIColor.backgroundColor, tagBorderColor: UIColor.GP_Blue_Color, contentInsets: UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5), labelContentInsets: UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3), labelHorizontalSpacing: 5, labelVerticalSpacing: 5)
        containerView.addSubview(tagsView!)
        priceLab.text = String(format: "￥%.2f起", mod.salePrice!)
        self.startLab.text = mod.basePlace!+"出发"
        
        
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
